﻿using System;
using System.Threading;
using System.Windows.Forms;

namespace JavaTool
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void bt_java_Click(object sender, EventArgs e)
        {
            configureJava(true);
        }
        /// <summary>
        /// 配置java环境
        /// </summary>
        /// <param name="isShowMessage">是否提示成功信息</param>
        private void configureJava(bool isShowMessage)
        {
            //Environment.SetEnvironmentVariable("CLASSPATH", @".;%JAVA_HOME%\lib", EnvironmentVariableTarget.Machine);
            //Environment.SetEnvironmentVariable("path", @"%JAVA_HOME%\lib;" + Environment.GetEnvironmentVariable("path"), EnvironmentVariableTarget.Machine);
            String javaPath = tb_java.Text.Trim().ToString();
            if (String.IsNullOrEmpty(javaPath))
            {
                MessageBox.Show("请安装如上eg填写您的Java安装目录");
                return;
            }
            Environment.SetEnvironmentVariable("JAVA_HOME", javaPath, EnvironmentVariableTarget.Machine);
            Environment.SetEnvironmentVariable("CLASSPATH", @".;%JAVA_HOME%\lib", EnvironmentVariableTarget.Machine);
            Environment.SetEnvironmentVariable("Path", String.Concat(@"%JAVA_HOME%\bin;", Environment.GetEnvironmentVariable("Path")), EnvironmentVariableTarget.Machine);
            if (isShowMessage)
                MessageBox.Show("Java环境配置成功");

        }

        private void bt_tomcat_Click(object sender, EventArgs e)
        {
            configureTomcat(true);
        }
        /// <summary>
        /// 配置Tomcat环境
        /// </summary>
        /// <param name="isShowMessage">是否提示成功信息</param>
        private void configureTomcat(bool isShowMessage)
        {
            String tomcatPath = tb_tomcat.Text.Trim().ToString();
            if (String.IsNullOrEmpty(tomcatPath))
            {
                MessageBox.Show("请安装如上eg填写您的Tomcat安装目录");
                return;
            }
            Environment.SetEnvironmentVariable("TOMCAT_HOME", tomcatPath, EnvironmentVariableTarget.Machine);
            if (isShowMessage)
                MessageBox.Show("Tomcat环境配置成功");
        }

        private void bt_maven_Click(object sender, EventArgs e)
        {
            configureMaven(true);
        }
        /// <summary>
        /// 配置Maven环境
        /// </summary>
        /// <param name="isShowMessage">是否提示成功信息</param>
        private void configureMaven(bool isShowMessage)
        {
            String mavenPath = tb_maven.Text.Trim().ToString();
            if (String.IsNullOrEmpty(mavenPath))
            {
                MessageBox.Show("请安装如上eg填写您的Maven安装目录");
                return;
            }
            Environment.SetEnvironmentVariable("MAVEN_HOME", mavenPath, EnvironmentVariableTarget.Machine);
            Environment.SetEnvironmentVariable("Path", String.Concat(@"%JAVA_HOME%\bin;", Environment.GetEnvironmentVariable("Path")), EnvironmentVariableTarget.Machine);
            if (isShowMessage)
                MessageBox.Show("Maven环境配置成功");
        }

        private void bt_smart_Click(object sender, EventArgs e)
        {
            configureSmart();
        }
        /// <summary>
        /// 根据所填写的环境变量智能配置
        /// </summary>
        ///  <param name="isShowMessage">是否提示成功信息</param>
        private void configureSmart()
        {
            String javaPath = tb_java.Text.Trim().ToString();
            String tomcatPath = tb_tomcat.Text.Trim().ToString();
            String mavenPath = tb_maven.Text.Trim().ToString();
            String result = String.Empty;
            if (!String.IsNullOrEmpty(javaPath))
            {
                configureJava(false);
                result = String.Concat(result, "Java环境 ");
            }
            if (!String.IsNullOrEmpty(tomcatPath))
            {
                configureTomcat(false);
                result = String.Concat(result, "Tomcat环境 ");
            }
            if (!String.IsNullOrEmpty(mavenPath))
            {
                configureMaven(false);
                result = String.Concat(result, "Maven环境 ");
            }
            result = String.Concat(result, "配置成功！");
            MessageBox.Show(result);
        }

        private void bt_check_Click(object sender, EventArgs e)
        {
            String javaResult = CmdUtils.write2Cmd("java -version");
            String mavenResult = CmdUtils.write2Cmd("mvn -v");
            javaResult = contactResult(javaResult, "microsoft");
            mavenResult = contactResult(mavenResult, "maven");
            MessageBox.Show(String.Concat(javaResult, mavenResult));
        }
        /// <summary>
        /// 根据关键字得出最后的结果,排除多余字段干扰
        /// </summary>
        /// <param name="javaResult"></param>
        /// <param name="keywords"></param>
        /// <returns></returns>
        private static string contactResult(string javaResult, string keywords)
        {
            string result = string.Empty;
            string[] javaResults = javaResult.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);

            for (int i = 0; i < javaResults.Length; i++)
            {
                if (!string.IsNullOrEmpty(javaResults[i]) && javaResults[i].ToLower().Contains(keywords.ToLower()))
                {

                }
                else
                {
                    result = string.Concat(result, @"\r\n", javaResults[i]);
                }
            }
            return result;
        }
    }
}
